package com.example.boot.rabbitmq.producer.config;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author root
 * @date 2021-03-06 14:21
 */
//@Configuration
@RequiredArgsConstructor
public class RabbitMqConfig {


    private final RabbitProperties rabbitProperties;


    /***
     * 创建一个死信交换器
     * @return 交换器
     */
    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange(rabbitProperties.getExchangeDlx());
    }



    /**
     * 声明死信队列
     * @return 队列
     */
    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable(rabbitProperties.getQueueDlx()).build();
    }



    /**绑定Exchange和queue，把消息重定向到业务queue*/
    @Bean
    Binding dlxBinding(DirectExchange delayExchange, Queue delayQueue){
        return BindingBuilder.bind(delayQueue)
                .to(delayExchange)
//                .with(rabbitProperties.getDlxKey())
                .with("com.#")
                ;
        //绑定，以PROCESS_QUEUE为routing key
    }





    /**
     * 创建一个普通交换器
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(rabbitProperties.getExchangeNormal());
    }


    /**创建用于业务的队列*/
    @Bean
    public Queue processQueue(){
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信队列Exchange
        args.put("x-dead-letter-exchange", rabbitProperties.getExchangeDlx());
//       x-dead-letter-routing-key    声明 死信队列抛出异常重定向队列的routingKey(TKEY_R)
        args.put("x-dead-letter-routing-key", rabbitProperties.getDlxKey());
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(rabbitProperties.getQueueNormal())
                .withArguments(args)
                .build();
    }




    @Bean
    Binding directBinding(DirectExchange directExchange, Queue processQueue){
        return BindingBuilder.bind(processQueue)
                .to(directExchange)
                .with(rabbitProperties.getDlxKey());
        //绑定，以PROCESS_QUEUE为routing key
    }






    /**
     * 创建一个普通交换器
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(rabbitProperties.getExchangeTopic());
    }

    /**创建用于业务的队列*/
    @Bean
    public Queue processQueueTopic(){
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信队列Exchange
        args.put("x-dead-letter-exchange", rabbitProperties.getExchangeDlx());
//       x-dead-letter-routing-key    声明 死信队列抛出异常重定向队列的routingKey(TKEY_R)
        args.put("x-dead-letter-routing-key", rabbitProperties.getSendOrderKey());
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(rabbitProperties.getQueueTopic())
                .withArguments(args)
                .build();
    }

    @Bean
    Binding topicBinding(TopicExchange topicExchange, Queue processQueueTopic){
        return BindingBuilder.bind(processQueueTopic)
                .to(topicExchange)
                .with(rabbitProperties.getOrderKey());
        //绑定，以PROCESS_QUEUE为routing key
    }

}
